package source.model;

import java.util.Hashtable;
import java.util.Vector;
import source.model.bonus.*;
import source.model.technology.*;

public class Player {

	private int id;
	private int ore;
	private int energy;
	private int energyUsed;
	private Vector<UnitMission> unitMissions;
	private UnitBonus[] bonus=new UnitBonus[4];
	private Vector<Technology> techsKnown=new Vector<Technology>();
	private Vector<Technology> techsAvailable=new Vector<Technology>();
	private Technology researching;
	private int totalResearchThisTurn;

	
	
	public Player(int id, int startingOre)
	{
		this.id = id;
		unitMissions = new Vector<UnitMission>();
		ore = startingOre;
		bonus[0]=new ColonistBonus();
		bonus[1]=new MeleeBonus();
		bonus[2]=new ExplorerBonus();
		bonus[3]=new RangedBonus();
		techsKnown=new Vector<Technology>();
		researching=null;
		techsAvailable.add(new UpgradeUnitArmor());
		techsAvailable.add(new UpgradeUnitDefensiveDamage());
		techsAvailable.add(new UpgradeUnitHealth());
		techsAvailable.add(new UpgradeUnitOffensiveDamage());
		techsAvailable.add(new UpgradeUnitRange());
		techsAvailable.add(new UpgradeUnitSpeed());
	}
	
	public int getID()
	{
		return this.id;
	}
	
	public int getOre() {
		return this.ore;
	}
	
	public void incrementOre(int ore) {
		this.ore += ore;
	}
	
	public boolean decrementOre(int ore) {
		if ((this.ore - ore) >= 0){
			this.ore -= ore;
			return true;
		}
		else {
			return false;
		}
	}
	
	public void incrementEnergy(int e)
	{
		energy += e;
	}
	public int incrementEnergyUsed(int e)
	{
		energyUsed += e;
		int over = energyUsed - energy;
		//Sean is crazy
		over = (over < 0)?0:over;
		return (over > e)?e:over;
	}
	
	public void newTurn()
	{
		energyUsed = 0;
		this.totalResearchThisTurn=0;
	}
	
	public void addUnitMission(UnitMission u)
	{
		unitMissions.add(u);
	}

	public void removeUnitMission(UnitMission u) {
		unitMissions.remove(u);
	}

	public Vector<UnitMission> getUnitMissions() {
		return unitMissions;
	}
	
	public UnitBonus[] getUnitBonuses(){
		return bonus;
	}
	
	public void research(){
		if(researching!=null)
			researching.research(totalResearchThisTurn);
	}
	
	public void addNextTech(Technology tech){
		techsKnown.add(tech);
	}
	
	public Vector<Technology> techsKnown(){
		return techsKnown;
	}
	
	public Vector<Technology> techsAvailable(){
		return techsAvailable;
	}
	
	public void modArmorBonus(int amount){
		for(int i=0; i<bonus.length; i++)
			bonus[i].increaseArmorBonus(amount);
	}
	
	public void modODBonus(int amount){
		for(int i=0; i<bonus.length; i++)
			bonus[i].increaseODBonus(amount);
	}
	
	public void modDDBonus(int amount){
		for(int i=0; i<bonus.length; i++)
			bonus[i].increaseDDBonus(amount);
	}
	
	public void modHealthBonus(int amount){
		for(int i=0; i<bonus.length; i++)
			bonus[i].increaseHealthBonus(amount);
	}
	
	public void modRangeBonus(int amount){
		for(int i=0; i<bonus.length; i++)
			bonus[i].increaseRangeBonus(amount);
	}
	
	public void modSpeedBonus(float amount){
		for(int i=0; i<bonus.length; i++)
			bonus[i].increaseSpeedBonus(amount);
	}
	
	public boolean finishedResearching(){
		
		return (researching==null)?
						false:
						researching.getResearchTillDone()<=0;
	}
	
	public Technology getResearching(){
		return researching;
	}
	
	public void researchNewTechnology(Technology tech){
		researching=tech;
	}
	
	public void incrementTotalResearch(int amount){
		totalResearchThisTurn+=amount;
	}
	
	public int getResearchThisTurn(){
		return totalResearchThisTurn;
	}
	
	public Hashtable<String, String> getViewData(){
		Hashtable<String, String> stats = new Hashtable<String, String>();
		
		stats.put("Ore", String.valueOf(getOre()));
		stats.put("Energy Used", String.valueOf(energyUsed));
		stats.put("Energy Total", String.valueOf(energy));
		
		return stats;
	}
}
